package com.jingyanzi.resallocation.service.impl;

import java.util.Calendar;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jingyanzi.resallocation.dao.ResAreaMapper;
import com.jingyanzi.resallocation.domain.AreaDO;
import com.jingyanzi.resallocation.entity.ResArea;
import com.jingyanzi.resallocation.entity.ResAreaExample;
import com.jingyanzi.resallocation.entity.ResAreaExample.Criteria;
import com.jingyanzi.resallocation.query.AreaQueryDO;
import com.jingyanzi.resallocation.result.ResultDO;
import com.jingyanzi.resallocation.result.ResultSupport;
import com.jingyanzi.resallocation.service.IAreaService;
import com.jingyanzi.resallocation.util.BeanUtilsExtends;


@Service("areaService")
public class AreaServiceImpl extends BaseServiceImpl implements IAreaService {

	@Autowired
	private ResAreaMapper areaMapper;

	@Override
	public ResultDO del(Integer[] ids) {
		ResultSupport result = new ResultSupport();
		if (!result.isSuccess())
			return result;
		for (int i = 0; i < ids.length; i++) {
			ResArea area = new ResArea();
			area.setId(ids[i]);
			area.setIsDelete(-1);
			int r = -1;

			try {
				r = areaMapper.updateByPrimaryKeySelective(area);
			} catch (Exception e) {
				e.printStackTrace();
				result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
				result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + "删除景区信息失败");
				result.setSuccess(false);
				return result;
			}

			if (r < 1) {
				result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
				result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + "删除景区信息失败");
				result.setSuccess(false);
				return result;
			}
		}
		return result;
	}

	@Override
	public ResultDO get(Integer id) {
		ResArea area = new ResArea();

		ResultSupport result = new ResultSupport();
		if (!result.isSuccess())
			return result;
		try {
			area = areaMapper.selectByPrimaryKey(id);
		} catch (Exception e) {
			e.printStackTrace();
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + "获取景区信息失败");
			result.setSuccess(false);
			return result;
		}

		if (area == null) {
			result.setErrorCode(ResultDO.AREA_GET_ERROR);
			result.setErrorMsg("不存在该景区信息");
			result.setSuccess(false);
			return result;
		}
		AreaDO areaDO = getAreaDO(area);
		result.setModel(ResultSupport.FIRST_MODEL_KEY, areaDO);
		return result;
	}

	@Override
	public ResultDO query(AreaQueryDO areaQueryDO) {
		ResultSupport result = new ResultSupport();

		List<ResArea> list = null;

		ResAreaExample example = new ResAreaExample();

		Criteria createCriteria = example.createCriteria();
		if (StringUtils.isEmpty(areaQueryDO.getName())) {
			createCriteria.andNameLike("%" + areaQueryDO.getName() + "%");
		}
		if (areaQueryDO.getGrade() != null && areaQueryDO.getGrade() != 0) {
			createCriteria.andGradeEqualTo(areaQueryDO.getGrade());
		}
		if (areaQueryDO.getStatus() != null && areaQueryDO.getStatus() != 0) {
			createCriteria.andStatusEqualTo(areaQueryDO.getStatus());
		}
		if (areaQueryDO.getCategory() != null && areaQueryDO.getCategory() != 0) {
			createCriteria.andCategoryEqualTo(areaQueryDO.getCategory());
		}
		createCriteria.andIsDeleteEqualTo(1);
		List<AreaDO> areaDOs = null;

		if (areaQueryDO.isPage()) {
			example.setLimitByPage(areaQueryDO.getStart() + "," + areaQueryDO.getPageRows());
			long count = areaMapper.countByExample(example);
			result.setModel(ResultSupport.SECOND_MODEL_KEY, count);
		}

		try {
			list = areaMapper.selectByExample(example);
		} catch (Exception e) {
			e.printStackTrace();
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + "获取景区信息失败");
			result.setSuccess(false);
			return result;
		}
		areaDOs = getAreaDOList(list);
		result.setModel(ResultSupport.FIRST_MODEL_KEY, areaDOs);
		return result;
	}

}
